package cn.zj.note.view;

import cn.zj.note.MainApp;
import cn.zj.note.todo.Record;
import cn.zj.note.todo.Todo;
import cn.zj.note.todo.TodoManager;
import cn.zj.note.util.StrFmtUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;

// 任务详情
public class TodoDetail extends VBox {
// 一个头部部分加一个ListView,头部部分显示标题，资源，状态，创建时间
    private Todo todo;
    private Label title = new Label();
    private Label status = new Label();
    private Label createTime = new Label();
    private Label resource = new Label();
    private ListView<Record> records = new ListView<>();
    private TextArea textArea = new TextArea();
    public TodoDetail(Todo todo) {
        if(null == todo){
            return;
        }
        this.todo = todo;
        HBox titleBox = new HBox(title, status, createTime, resource);
        titleBox.setSpacing(10);
        records.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if(null == newValue){
                return;
            }
            final Clipboard clipboard = Clipboard.getSystemClipboard();
            final ClipboardContent content = new ClipboardContent();
            content.putString(newValue.getContent());
            clipboard.setContent(content);
            System.out.println("Selected item: " + newValue.getContent());
        });
        getChildren().addAll(titleBox, records, textArea);
        VBox.setVgrow(records, Priority.ALWAYS);
//        textArea.setMaxWidth(680);
        textArea.setWrapText(true);
        textArea.setOnKeyPressed(event -> {
            if (event.isControlDown() && event.getCode() == KeyCode.ENTER) {
                String inputText = textArea.getText();
                if("完成".equals(inputText) || "done".equalsIgnoreCase(inputText)){
                    this.todo.setStatus(true);
                    MainApp.refreshListView();
                }else if("进行中".equals(inputText) || "todo".equalsIgnoreCase(inputText)){
                    this.todo.setStatus(false);
                    MainApp.refreshListView();
                }
                Record record = new Record(inputText);
                this.todo.addRecord(record);
                this.update(this.todo);
                textArea.clear();
                System.out.println("=========update======"+this.todo);
                TodoManager.getInstance().saveData(this.todo);
                records.scrollTo(record);
            }
        });
        records.setCellFactory(param -> new RecordListCell());
    }

    public void update(Todo todo){
        if(null == todo){
            return;
        }
        this.todo = todo;
        title.setText(todo.getTitle());
        status.setText(todo.isStatus()?"已完成":"进行中");
        createTime.setText(StrFmtUtil.fmtDate(todo.getCreateTime()));
        ObservableList<Record> items = FXCollections.observableArrayList(todo.getRecords());
        records.setItems(items);
        if(!todo.getRecords().isEmpty()){
            records.scrollTo(todo.getRecords().get(items.size()-1));
        }
    }

    public Todo getTodo() {
        return todo;
    }

    public void updateNow() {
        update(this.todo);
    }
}
